package com.test.mchelepesa.util_rice.helper_rice;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.test.mchelepesa.activity_fragment_rice.base_rice.BaseApplication_rice;
import com.test.mchelepesa.util_rice.StringUtil_rice;


/**
 * 用户信息管理
 * 初始化本地token/mobile等数据
 */
public class UserInfoHelper_rice {

    public static final String SP_KEY_TOKEN_rice = "token_rice";
    public static final String SP_KEY_MOBILE_rice = "phoneNum_rice";
    public static final String SP_KEY_USERID_rice = "userid_rice";
    public static final String SP_KEY_IS_AD_SOURCE_rice = "isAdSource_rice";
    public static final String SP_KEY_COUNTRY_WORD = "country_word";
    public static final String SP_KEY_COUNTRY_NAME = "country_name";

    public static final String SP_KEY_STARNORMAL_COUNT_rice = "startNormalCount_rice";
    public static final String SP_KEY_STARACTIVE_COUNT_rice = "activeNormalCount_rice";

    public static final String LOGIN_TYPE_REGISTER_rice = "REGISTER";
    public static final String LOGIN_TYPE_OLD_rice = "OLD";

    public static final String SP_KEY_LANGUAGE = "rice_language";

    /**
     * 用户是否选择过额度
     */
    public static String USER_HAD_SELECTED_AMOUNT_FLAG_rice = "NO";

    /**
     * 全局sp
     */
    private final SharedPreferences mSharedPreferences_rice;

    /**
     * 登录手机号
     */
    private String mMobile_rice;
    /**
     * 登录token
     */
    private String mToken_rice;
    /**
     * 登录user_id
     */
    private String mUserId_rice;
    /**
     * 是否为渠道用户，是的话，走贷款流程，不是的话，走壳子包
     */
    private String mIsAdSource;

    /**
     * 登录接口字段
     */
    private String mCountryWord;
    /**
     * 国家名
     */
    private String mCountryName;
    /**
     * 语言（坦桑）
     */
    private String language;

    private String verificationFlag;
    private String showDropButton;
    private String dropType;

    private int starNormalCount,starActiveCount;

    private static class InnerUserInfoHelper {
        private static final UserInfoHelper_rice instance = new UserInfoHelper_rice();
    }

    public static UserInfoHelper_rice getInstance() {
        return InnerUserInfoHelper.instance;
    }

    private UserInfoHelper_rice() {
        mSharedPreferences_rice = BaseApplication_rice.gApplication_rice().getSharedPreferences(DeviceHelper_rice.getPackageName(), Context.MODE_PRIVATE);
        this.mMobile_rice = mSharedPreferences_rice.getString(SP_KEY_MOBILE_rice, "");
        this.mIsAdSource = mSharedPreferences_rice.getString(SP_KEY_IS_AD_SOURCE_rice, "");
        this.mToken_rice = mSharedPreferences_rice.getString(SP_KEY_TOKEN_rice, "");
        this.mUserId_rice = mSharedPreferences_rice.getString(SP_KEY_USERID_rice, "");
        this.mCountryWord = mSharedPreferences_rice.getString(SP_KEY_COUNTRY_WORD, "");
        this.mCountryName = mSharedPreferences_rice.getString(SP_KEY_COUNTRY_NAME, "");
        this.starNormalCount = mSharedPreferences_rice.getInt(SP_KEY_STARNORMAL_COUNT_rice,0);
        this.starActiveCount = mSharedPreferences_rice.getInt(SP_KEY_STARACTIVE_COUNT_rice,0);
        this.language = mSharedPreferences_rice.getString(SP_KEY_LANGUAGE,"");
    }

    public void init(Context context) {}

    //判断是否已经登录
    public boolean didLogin_rice() {
        return (!TextUtils.isEmpty(this.mToken_rice));
    }

    public String getMobil_rice() {
        mMobile_rice = StringUtil_rice.getSafeString(mMobile_rice);
        return mMobile_rice;
    }

    @SuppressLint("ApplySharedPref")
    public void setMobile_rice(String mobile) {
        this.mMobile_rice = StringUtil_rice.getSafeString(mobile);
        mSharedPreferences_rice.edit().putString(SP_KEY_MOBILE_rice, this.mMobile_rice).commit();
    }

    public String getToken_rice() {
        mToken_rice = StringUtil_rice.getSafeString(mToken_rice);
        return mToken_rice;
    }

    public String getUserId_rice() {
        mUserId_rice = StringUtil_rice.getSafeString(mUserId_rice);
        return mUserId_rice;
    }

    @SuppressLint("ApplySharedPref")
    public void setToken_rice(String token) {
        this.mToken_rice = StringUtil_rice.getSafeString(token);
        mSharedPreferences_rice.edit().putString(SP_KEY_TOKEN_rice, this.mToken_rice).commit();
    }

    public void setVerificationFlag(String verificationFlag) {
        this.verificationFlag = verificationFlag;
    }

    public String getVerificationFlag() {
        if (TextUtils.isEmpty(verificationFlag)) return "";
        return verificationFlag;
    }

    @SuppressLint("ApplySharedPref")
    public void setUserId_rice(String userId) {
        this.mUserId_rice = StringUtil_rice.getSafeString(userId);
        mSharedPreferences_rice.edit().putString(SP_KEY_USERID_rice, this.mUserId_rice).commit();
    }

    public String getIsAdSourceString_rice() {
        return StringUtil_rice.getSafeString(mIsAdSource);
    }

    public boolean isAdSource_rice() {
        return StringUtil_rice.getSafeString(mIsAdSource).equalsIgnoreCase("YES");
    }

    public void sShowDropButton(String showDropButton) {
        this.showDropButton = showDropButton;
    }

    public void sDropType(String dropType) {
        this.dropType = dropType;
    }

    public String dropType() {
        if (TextUtils.isEmpty(dropType)) return "";
        return dropType;
    }

    public boolean showDropButton() {
        return "YES".equalsIgnoreCase(showDropButton) || "true".equalsIgnoreCase(showDropButton);
    }

    @SuppressLint("ApplySharedPref")
    public void setIsAdSource_rice(String isAdSource) {
        this.mIsAdSource = StringUtil_rice.getSafeString(isAdSource);
        mSharedPreferences_rice.edit().putString(SP_KEY_IS_AD_SOURCE_rice, this.mIsAdSource).commit();
    }

    public int getStarActiveCount_rice() {
        return starActiveCount;
    }

    public int getStarNormalCount_rice() {
        return starNormalCount;
    }

    @SuppressLint("ApplySharedPref")
    public void addStarNormalCount_rice(){
        starNormalCount++;
        mSharedPreferences_rice.edit().putInt(SP_KEY_STARNORMAL_COUNT_rice,starNormalCount).commit();
    }

    public String getCountryName() {
        mCountryName = StringUtil_rice.getSafeString(mCountryName);
        return mCountryName;
    }

    public String getCountryWord() {
        mCountryWord = StringUtil_rice.getSafeString(mCountryWord);
        return mCountryWord;
    }

    @SuppressLint("ApplySharedPref")
    public void setCountryWord(String countryWord) {
        this.mCountryWord = StringUtil_rice.getSafeString(countryWord);
        mSharedPreferences_rice.edit().putString(SP_KEY_COUNTRY_WORD, this.mCountryWord).commit();
    }

    @SuppressLint("ApplySharedPref")
    public void setCountryName(String countryName) {
        this.mCountryName = StringUtil_rice.getSafeString(countryName);
        mSharedPreferences_rice.edit().putString(SP_KEY_COUNTRY_NAME, this.mCountryName).commit();
    }

    @SuppressLint("ApplySharedPref")
    public void addStarActiveCount_rice(){
        starActiveCount++;
        mSharedPreferences_rice.edit().putInt(SP_KEY_STARACTIVE_COUNT_rice,starActiveCount).commit();
    }

    public String getLanguage() {
        return StringUtil_rice.getSafeString(language);
    }

    @SuppressLint("ApplySharedPref")
    public void setLanguage(String language) {
        this.language = language;
        mSharedPreferences_rice.edit().putString(SP_KEY_LANGUAGE,this.language).commit();
    }
}
